<div class="weuser-body">

    <script type="text/html" id="toolbar">
        <div class="layui-inline">
            <button class="layui-btn" lay-event="download_user">
                <i class="layui-icon layui-icon-download-circle"></i>获取用户
            </button>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input  id="search_user" name="name" class="layui-input" type="text" placeholder="请输入用户姓名">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <button type="button" class="layui-btn layui-btn"  lay-event="search-btn">搜索用户</button>
            </div>
        </div>
    </script>

    <script type="text/html" id="opt_bar">
        <a href="javascript:;" title="图片权限" lay-event="img_priv">
            <i class="layui-icon">&#xe64a;</i>
        </a>
<!--        <a href="javascript:;" title="删除" lay-event="delete">-->
<!--            <i class="layui-icon">&#xe640;</i>-->
<!--        </a>-->
    </script>

    <table id="user_table" lay-filter="user_table"></table>

</div>
<script>
    layui.extend({
        admin: '<?php echo Yii::app()->baseUrl?>/static/admin/js/admin'
    });
    layui.use(['table','jquery','admin'], function() {
        var table = layui.table;
        var $ = layui.jquery;
        var load_index = layer.load();
        table.render({
            elem: '#user_table',
            method: 'post',
            url: '<?php echo $this->createUrl('user/list')?>', //数据接口
            page: true, //开启分页
            toolbar: '#toolbar',
            id: 'user_table',
            defaultToolbar:[],
            loading:false,
            cols: [[ //表头
                // {type: 'checkbox', fixed: 'left'},
                {field: 'userid', title: 'ID',align:'center'},
                {field: 'name', title: '用户名',align:'center'},
                {field: 'avatar', title: '头像',align:'center', templet:function (row) {
                        return '<img src="' + row.avatar + '" width="35" height="45"/>'
                    }},
                {field: 'gender', title: '性别',align:'center', templet:function (row) {
                        switch (row.gender) {
                            case '1' :
                                return '男';
                            case '2':
                                return '女';
                            case '0':
                            default:
                                return '未定义';
                        }
                    }},
                {field: 'position', title: '职位',align:'center'},
                {field: 'mobile', title: '手机号',align:'center'},
                {title: '状态',align:'center',templet:function (row) {
                        switch (row.status) {
                            case '1' :
                                return '已关注';
                            case '2':
                                return '已冻结';
                            case '4':
                            default:
                                return '未定义';
                        }
                    }},
                {fixed: 'right', title:'操作', toolbar: '#opt_bar',align:'center'}
            ]],
            done:function (data) {
                layer.close(load_index);
            }
        });

        table.on('toolbar(user_table)', function (obj) {
            switch (obj.event) {
                case 'download_user' :
                    var index = layer.load();
                    $.get('<?php echo $this->createUrl('user/getList')?>', function (ret) {
                        layer.close(index);
                        if (ret.code == 0) {
                            table.reload('user_table');
                            layer.msg(ret.message,{time:1000, icon:1});
                        } else {
                            layer.msg(ret.message,{time:1500, icon:2});
                        }
                    });
                    break;
                case 'search-btn':
                    search();
                    break;
                default:
                    return;
            }
        });
        table.on('tool(user_table)', function (row) {
            switch (row.event) {
                case 'img_priv':
                    location.href =  '<?php echo $this->createUrl('user/userImg')?>?userid='+row.data.userid;
                    break;
                default :
                    break;
            }
        });

        function search() {
            var name=$("#search_user").val();
            load_index = layer.load();
            table.reload('user_table',{
                where:{
                    name:name}
            });
            $("#search_user").val(name);
            return false;
        }
        $("#search_user").keyup(function (event) {
            if (event.keyCode == 13) {
                search();
            }
        });
    });
</script>